package com.foreknow.hospital.dao.impl;

import com.foreknow.hospital.dao.SetmealDao;
import com.foreknow.hospital.po.CheckItem;
import com.foreknow.hospital.po.Setmeal;
import com.foreknow.hospital.po.SetmealDetailed;
import com.foreknow.hospital.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SetmealDaoImp implements SetmealDao {
    private Connection con;
    private PreparedStatement pre;
    private ResultSet rs;
    @Override
    public List<Setmeal> getSetmealByType(int type) throws SQLException {
       List<Setmeal> list = new ArrayList<Setmeal>();
        Setmeal setmeal = null;

        List<SetmealDetailed> sdList = new ArrayList<>();
        String sql = "SELECT s.smId,s.name,s.price,s.type, d.sdId,c.ciId,c.ciName,c.ciContent,c.meaning\n" +
                "from setmeal s ,setmealdetailed d,checkitem c " +
                "WHERE s.smId=d.smId and c.ciId=d.ciId  and s.type=?";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            //把问号替换为具体的值
            pre.setInt(1, type);
            Integer currentSmId=null;
            //3.执行查询操作
            rs = pre.executeQuery();
            while (rs.next()) {
                while (rs.next()) {
                    Integer smId = rs.getInt("smId");
                    // 初始化套餐基础信息
                    if (setmeal == null || !smId.equals(currentSmId)) {
                        setmeal = new Setmeal();
                        setmeal.setSmId(rs.getInt("smId"));
                        setmeal.setName(rs.getString("name"));
                        setmeal.setType(rs.getInt("type"));
                        setmeal.setPrice(rs.getInt("price"));
                        setmeal.setSdList(sdList);
                        list.add(setmeal);
                        currentSmId=smId;
                    }

                    // 封装明细项（SetmealDetailed）
                    Integer sdId = rs.getInt("sdId");
                    if (sdId != null) {
                        SetmealDetailed sd = new SetmealDetailed();
                        sd.setSdId(sdId);
                        sd.setSmId(rs.getInt("smId"));
                        sd.setCiId(rs.getInt("ciId"));

                        // 封装检查项（CheckItem）
                        CheckItem ci = new CheckItem();
                        ci.setCiId(rs.getInt("ciId"));
                        ci.setCiName(rs.getString("ciName"));
                        ci.setCiContent(rs.getString("ciContent"));
                        ci.setMeaning(rs.getString("meaning"));
                        sd.setCheckItem(ci);
                        setmeal.getSdList().add(sd);

                    }
                }
            }
        } finally {
            DBUtil.close(pre);
        }
        return list;
    }

    @Override
    public Setmeal getSetmealById(Setmeal setmeal) throws SQLException {
        int smId = setmeal.getSmId();
        Setmeal setmeal1 = new Setmeal();
        String sql = "SELECT * from setmeal where smId=?";

        try {
            con = DBUtil.getConnection();
            pre = con.prepareStatement(sql);
            pre.setInt(1, smId);
            rs = pre.executeQuery();

            if (rs.next()) {
                setmeal1.setSmId(rs.getInt("smId"));
                setmeal1.setName(rs.getString("name"));
                setmeal1.setType(rs.getInt("type"));
                setmeal1.setPrice(rs.getInt("price"));
            }
        } finally {
            DBUtil.close(pre);
        }

        return setmeal1;
    }
}



